%
clear; close all;

load('/home/wks/mywks/SR-Works/code/FSRCNN_Train_Test/FSRCNN_test/model/FSRCNN/x3.mat');
my_solver = '/home/wks/mywks/SR-Works/code/FSRCNN_Train_Test/FSRCNN/FSRCNN_solver.prototxt';
%% Show our solver
mycaffe.reset_all();
solver = caffe.Solver(my_solver);
layers=length(weights_conv);

%%  all kinds layers
allname=solver.net.blob_names;  % weight and biase for the same layer
name=allname(3:end-1);

allrelulayer=solver.net.layer_names;  % weight_relu
prelu_name = allrelulayer(3:2:end-2);

%% except the last layer
for layer = 1 : layers
    %%  get the params from the mat
    weight = weights_conv{layer,1};
    prelu = prelu_conv{layer,1};
    biases = biases_conv{layer,1};
    
    %% for weights reshape
    [channel, kernalsize, filternums] = size(weight);
    kernalsize = sqrt(kernalsize);
    
    myweight = zeros(kernalsize,kernalsize,channel,filternums);  % the weight need update
    for i = 1 : filternums % for every filter numbers
        myweight(:,:,:,i) = reshape(weight(:,:,i)',kernalsize,kernalsize,channel);
    end
    
    %% update that caffemodel
    if layer < layers  % Finally layer
        solver.net.params(prelu_name{layer}, 1).set_data(prelu); % set biases
    end
    
    solver.net.params(name{layer}, 1).set_data(myweight); % set weights
    solver.net.params(name{layer}, 2).set_data(biases); % set biases
end

%%
solver.net.save('/home/wks/mywks/SR-Works/code/FSRCNN_Train_Test/FSRCNN/models/myFSRCNN.caffemodel');
disp('Done');
